import router from '@/router/'
import store from '@/store'
import $customer from '@/api/customer'
import { validator } from '@/utils/common'
import common from '@/utils/common'
function doFilter () {
  router.beforeEach((to, from, next) => {
    console.log(from)
    console.log(to)
    if (!to.meta.thirdAuth) {
      return next()
    }
    let third_redirect_code = common.getUrlCode().code
    if (!validator.isEmpty(third_redirect_code)) {
      return next()
    }
    // 根据不同的thirdType和后端交互，创建thirdToken
    $customer.thirdAuth({
      scope: to.meta.thirdScopeType || '',
      code: third_redirect_code,
    }).then(res => {
      if (validator.isEmpty(res.thirdToken)) {
        store.dispatch('setThirdToken', res.thirdToken)
        if ((!validator.isEmpty(store.getters.getNickName)) && (!validator.isEmpty(store.getters.getImgUrl))) {
					$customer.thirdInfo({
						thirdToken: store.getters.getThirdToken
					}).then(res1 => {
						if (validator.isEmpty(res1.thirdUserName) || validator.isEmpty(res1.thirdImageUrl)) {
							store.dispatch('setThirdInfo', {
								nickName: res1.thirdUserName,
								imgUrl: res1.thirdImageUrl
							})
						}
						return next()
					})
				}else{
					return next()
				}
      }else {
        return next()
      }
    })
  })
}

export default { doFilter }